﻿Imports System.Data.OleDb


Public Class details
    Inherits System.Web.UI.Page

    Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return
        Label1.Visible = False
        'meaning, if it is a post back, then we jump out of the function and do not run the rest below
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        Dim sql = "INSERT INTO CategoriesViewedByUsers (Catfkid,UName) VALUES (@f1,@f2)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()

        'Dim sql2 = "INSERT INTO CategoriesViewedByUsers (CatFKID,Userfkid) VALUES (@f1,@f2)"
        'Dim cmd2 = New OleDbCommand(sql2, conn)
        'cmd2.Parameters.AddWithValue("@f1", Request.Params("ID"))
        'cmd2.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        'cmd2.ExecuteNonQuery()


        Dim sql5 As String = "SELECT * FROM AverageRating WHERE Pic_id=@f1"
        Dim conn5 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        conn5.Open()
        Dim cmd5 As OleDbCommand = New OleDbCommand(sql5, conn5)
        cmd5.Parameters.AddWithValue("@f1", Request.QueryString("id"))
        Dim dataReader = cmd5.ExecuteReader
        dataReader.Read()
        Try
            If Not IsDBNull(dataReader("AverageRating")) Then lblAverageRatings.Text = dataReader("AverageRating")
        Catch ex As Exception
            lblAverageRatings.Text = 0
        End Try

        Dim int As Single = CInt(lblAverageRatings.Text)
    End Sub


    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        Dim sql = "insert into Comments (Author, Subject, Message, pic_id) values (@f1,@f2,@f3,@f4)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Subject.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Message.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("id"))
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("details.aspx?ID=" & Request.Params("id"))
    End Sub

    Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim catId = Request.Params("ID")
        Session("CategoryID") = catId
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        conn.Open()

        Dim newTags = tb_NewTags.Text.Split(",")

        For Each newTag As String In newTags
            Dim isTagThereCmd As New OleDbCommand("SELECT * FROM Tags WHERE Tag=@f1", conn)
            isTagThereCmd.Parameters.AddWithValue("@f1", newTag)
            Dim dr = isTagThereCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagCmd As New OleDbCommand("INSERT INTO Tags (Tag) VALUES (@f1)", conn)
                addTagCmd.Parameters.AddWithValue("@f1", newTag)
                addTagCmd.ExecuteNonQuery()
            End If
            Dim findNewTagIDCmd As New OleDbCommand("SELECT ID FROM Tags WHERE Tag=@f1", conn)
            findNewTagIDCmd.Parameters.AddWithValue("@f1", newTag)
            Dim newTagDr = findNewTagIDCmd.ExecuteReader()
            newTagDr.Read()
            Dim newID = newTagDr.Item("ID")
            Dim makeAssociationCmd As New OleDbCommand("INSERT INTO CategoryTagJunction (TagFKID,CatFKID) VALUES (@f1,@f2)", conn)
            makeAssociationCmd.Parameters.AddWithValue("@f1", newID)
            makeAssociationCmd.Parameters.AddWithValue("@f2", catId)
            makeAssociationCmd.ExecuteNonQuery()


        Next






        For Each li As ListItem In CheckBoxList1.Items
            If (li.Selected = True) Then
                Dim sql = "INSERT INTO CategoryTagJunction (TagFKID,CatFKID) VALUES (@f1,@f2)"
                Dim cmd As New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", li.Value)
                cmd.Parameters.AddWithValue("@f2", catId)
                cmd.ExecuteNonQuery()
            End If
        Next
        conn.Close()
        Response.Redirect("details.aspx?ID=" & Request.Params("id"))
    End Sub

    Protected Sub RatingControlChanged(ByVal sender As Object, ByVal e As AjaxControlToolkit.RatingEventArgs)
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)

        Dim rmsql = "DELETE FROM RATING WHERE Rater=@f1 AND Pic_id=@f2"
        Dim rmcmd As New OleDbCommand(rmsql, conn)
        conn.Open()
        rmcmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        rmcmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        rmcmd.ExecuteNonQuery()
        conn.Close()


        Dim sql = "INSERT INTO RATING (Rating,Pic_id,Rater) VALUES (@f1,@f2,@f3)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()



        Dim sql5 As String = "SELECT * FROM AverageRating WHERE Pic_id=@f1"
        Dim conn5 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        conn5.Open()
        Dim cmd5 As OleDbCommand = New OleDbCommand(sql5, conn5)
        cmd5.Parameters.AddWithValue("@f1", Request.QueryString("id"))
        Dim dataReader = cmd5.ExecuteReader
        dataReader.Read()
        Try
            If Not IsDBNull(dataReader("AverageRating")) Then lblAverageRatings.Text = dataReader("AverageRating")
        Catch ex As Exception
            lblAverageRatings.Text = 0
        End Try

        Dim int As Single = CInt(lblAverageRatings.Text)
    End Sub
End Class